package dhm.com.hpx.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.VisitorAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.GetUserVisitor;
import dhm.com.hpx.entity.VisitorBean;
import dhm.com.hpx.utils.SpUtils;

public class VisitorActivity extends BaseActivity implements LoginContract.IView {


    @BindView(R.id.add_visitor)
    TextView addVisitor;
    @BindView(R.id.today_visitor)
    TextView todayVisitor;
    @BindView(R.id.all_visitor)
    TextView allVisitor;
    @BindView(R.id.recy)
    RecyclerView recy;


    private PressenterImpl pressenter;
    private String uid;
    private VisitorAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_visitor;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        uid = SpUtils.getString(this, "uid");

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        adapter = new VisitorAdapter(this);
        recy.setAdapter(adapter);

        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("user_id",uid);
        pressenter.sendMessage(this,Constant.visitor_list,map, GetUserVisitor.class);


    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetUserVisitor){
            GetUserVisitor getUserVisitor= (GetUserVisitor) data;
            if (getUserVisitor.getCode()==1){
                adapter.setList(getUserVisitor.getData());
            }else {
                recy.setVisibility(View.GONE);
            }
            Map<String,String> map=new HashMap<>();
            map.put("token",Constant.TOKEN);
            map.put("user_id",uid);
            pressenter.sendMessage(VisitorActivity.this,Constant.visitor_num,map, VisitorBean.class);

        }else if (data instanceof VisitorBean){
            VisitorBean visitorBean= (VisitorBean) data;
            if (visitorBean.getCode()==1){
                addVisitor.setText(visitorBean.getData().getToday_visitor_new_num()+"");
                todayVisitor.setText(visitorBean.getData().getToday_visitor_num()+"");
                allVisitor.setText(visitorBean.getData().getVisitor_zong()+"");
            }

            Map<String,String> map=new HashMap<>();
            map.put("token",Constant.TOKEN);
            map.put("user_id",uid);
            pressenter.sendMessage(VisitorActivity.this,Constant.visitor_read,map, Bean.class);
        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
